package com.shop2cn.iapi.prodstockmgr.testcase;

import java.io.IOException;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.shop2cn.iapi.prodstockmgr.parameter.GetStockInInventoryListBean;
import com.shop2cn.iapi.prodstockmgr.service.GetStockInInventoryListCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.GodfreightmgrService;
import com.ymttest.business.service.Shop2cnStockCallService;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.YMTDateUtil;
/**
*入库单列表Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetStockInInventoryList{
	private static GetStockInInventoryListBean getstockininventorylistBean;
	private static GetStockInInventoryListCall getstockininventorylistCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("入库单列表Test");
		try {
			GodfreightmgrService.deleteBySellerId(rootUserId);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@Before
	public void caseUp() {
		getstockininventorylistBean=new GetStockInInventoryListBean();
		getstockininventorylistCall=new GetStockInInventoryListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getstockininventorylistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	// 渠道商
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";
	// 渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	// vip渠道
	private static int vipRootUserId = AgentUsers.vipRootUserId;
	private static String vipRootUserName = "NickautoTest";
	private static int csId = AgentUsers.csId;
	// vip渠道->代理
	private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
	private static String vipAgentUserName1 = "dgsq408897016";
	private static int csId1 = AgentUsers.csId1;
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockInInventoryList_001() {
		Logger.start(true,"查新新增的入库单-默认type=0全部，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			String stockIn = Shop2cnStockCallService.stockIn(rootUserId, productId, 10);
			getstockininventorylistBean.setSellerId(rootUserId);
			getstockininventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockininventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			getstockininventorylistBean.setPageIndex(1);
			getstockininventorylistBean.setPageSize(50);
			getstockininventorylistCall.setData(getstockininventorylistBean);
			getstockininventorylistCall.callService();			
			Logger.verifyEquals("200", getstockininventorylistCall.getString("code"), "验证code");
			Logger.verifyEquals(true, getstockininventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt()>0, "验证查询到的条数");
			
			JsonArray asJsonArray = getstockininventorylistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("inventoryList");
			asJsonArray.forEach(b->{
				JsonObject inventory = b.getAsJsonObject();
				String inventoryNo = inventory.get("inventoryNo").getAsString();
				if(inventoryNo.equals(stockIn)){
					Logger.verifyEquals(10, inventory.get("totalNum").getAsInt(), "判断入库数量");
					//Logger.verifyEquals(10, inventory.get("totalCost").getAsInt(), "判断总成本价");
					//修改总成本价格 数量 *单个价格 wuwen 20200205
					Logger.verifyEquals(10 * 10, inventory.get("totalCost").getAsInt(), "判断总成本价");
				}
			});
			
			
		} catch (Exception e) {
			Logger.fail(e);

		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockInInventoryList_002() {
		Logger.start(true,"时间范围不正确，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Shop2cnStockCallService.stockIn(rootUserId, productId, 10);
			getstockininventorylistBean.setSellerId(rootUserId);
			getstockininventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			getstockininventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockininventorylistBean.setPageIndex(1);
			getstockininventorylistBean.setPageSize(50);
			
			getstockininventorylistCall.setData(getstockininventorylistBean);
			getstockininventorylistCall.callService();			
			Logger.verifyEquals("200", getstockininventorylistCall.getString("code"), "验证code");
			Logger.verifyEquals(0, getstockininventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt(), "验证查询到的条数");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockInInventoryList_003() {
		Logger.start(true,"商户id为空，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Shop2cnStockCallService.stockIn(rootUserId, productId, 10);
			getstockininventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockininventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			getstockininventorylistBean.setPageIndex(1);
			getstockininventorylistBean.setPageSize(50);
			
			getstockininventorylistCall.setData(getstockininventorylistBean);
			getstockininventorylistCall.callService();			
			Logger.verifyEquals("400", getstockininventorylistCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockInInventoryList_004() {
		Logger.start(true,"查新别人的的入库单，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Shop2cnStockCallService.stockIn(rootUserId, productId, 10);
			getstockininventorylistBean.setSellerId(vipRootUserId);
			getstockininventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockininventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			getstockininventorylistBean.setPageIndex(1);
			getstockininventorylistBean.setPageSize(50);
			
			getstockininventorylistCall.setData(getstockininventorylistBean);
			getstockininventorylistCall.callService();			
			Logger.verifyEquals("200", getstockininventorylistCall.getString("code"), "验证code");
			Logger.verifyEquals(0, getstockininventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt(), "验证查询到的条数");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockInInventoryList_005() {
		Logger.start(true,"代理商查询渠道商的入库单，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Shop2cnStockCallService.stockIn(rootUserId, productId, 10);
			getstockininventorylistBean.setSellerId(agentUserId1);
			getstockininventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockininventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			getstockininventorylistBean.setPageIndex(1);
			getstockininventorylistBean.setPageSize(50);
			
			getstockininventorylistCall.setData(getstockininventorylistBean);
			getstockininventorylistCall.callService();			
			Logger.verifyEquals("200", getstockininventorylistCall.getString("code"), "验证code");
			Logger.verifyEquals(0, getstockininventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt(), "验证查询到的条数");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockInInventoryList_007() {
		Logger.start(true,"pageIndex为空，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Shop2cnStockCallService.stockIn(rootUserId, productId, 10);
			getstockininventorylistBean.setSellerId(rootUserId);
			getstockininventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockininventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			getstockininventorylistBean.setPageSize(50);
			
			getstockininventorylistCall.setData(getstockininventorylistBean);
			getstockininventorylistCall.callService();			
			Logger.verifyEquals("500", getstockininventorylistCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockInInventoryList_008() {
		//0全部，1手动入库，2调整入库
		Logger.start(true,"查新新增的入库单-type=1手动入库，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//String stockIn = Shop2cnStockCallService.stockIn(rootUserId, productId, 10);
			//0手动入库，1调整库存
			String stockIn = Shop2cnStockCallService.stockIn(rootUserId, productId, 10,0);
			//0全部，1手动入库，2调整入库
			getstockininventorylistBean.setType(1);
			getstockininventorylistBean.setSellerId(rootUserId);
			getstockininventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockininventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			getstockininventorylistBean.setPageIndex(1);
			getstockininventorylistBean.setPageSize(50);
			getstockininventorylistCall.setData(getstockininventorylistBean);
			getstockininventorylistCall.callService();			
			Logger.verifyEquals("200", getstockininventorylistCall.getString("code"), "验证code");
			Logger.verifyEquals(true, getstockininventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt()>0, "验证查询到的条数");
			
			JsonArray asJsonArray = getstockininventorylistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("inventoryList");
			asJsonArray.forEach(b->{
				JsonObject inventory = b.getAsJsonObject();
				String inventoryNo = inventory.get("inventoryNo").getAsString();
				if(inventoryNo.equals(stockIn)){
					Logger.verifyEquals(10, inventory.get("totalNum").getAsInt(), "判断入库数量");
					//Logger.verifyEquals(10, inventory.get("totalCost").getAsInt(), "判断总成本价");
					//修改总成本价格 数量 *单个价格 wuwen 20200205
					Logger.verifyEquals(10 * 10, inventory.get("totalCost").getAsInt(), "判断总成本价");
					//清单类型，1手动入库，2手动出库，3发货出库，4调整入库，5调整出库
					Logger.verifyEquals(1, inventory.get("type").getAsInt(), "验证返回的type=1调整入库");
				}
			});
			
			
		} catch (Exception e) {
			Logger.fail(e);

		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockInInventoryList_009() {
		//0全部，1手动入库，2调整入库
		Logger.start(true,"查新新增的入库单-type=2调整入库，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//String stockIn = Shop2cnStockCallService.stockIn(rootUserId, productId, 10);
			//0手动入库，1调整库存
			String stockIn = Shop2cnStockCallService.stockIn(rootUserId, productId, 10,1);
			//0全部，1手动入库，2调整入库
			getstockininventorylistBean.setType(2);
			getstockininventorylistBean.setSellerId(rootUserId);
			getstockininventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockininventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			getstockininventorylistBean.setPageIndex(1);
			getstockininventorylistBean.setPageSize(50);
			getstockininventorylistCall.setData(getstockininventorylistBean);
			getstockininventorylistCall.callService();			
			Logger.verifyEquals("200", getstockininventorylistCall.getString("code"), "验证code");
			Logger.verifyEquals(true, getstockininventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt()>0, "验证查询到的条数");
			
			JsonArray asJsonArray = getstockininventorylistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("inventoryList");
			asJsonArray.forEach(b->{
				JsonObject inventory = b.getAsJsonObject();
				String inventoryNo = inventory.get("inventoryNo").getAsString();
				if(inventoryNo.equals(stockIn)){
					Logger.verifyEquals(10, inventory.get("totalNum").getAsInt(), "判断入库数量");
					//Logger.verifyEquals(10, inventory.get("totalCost").getAsInt(), "判断总成本价");
					//修改总成本价格 数量 *单个价格 wuwen 20200205
					Logger.verifyEquals(10 * 10, inventory.get("totalCost").getAsInt(), "判断总成本价");
					//清单类型，1手动入库，2手动出库，3发货出库，4调整入库，5调整出库
					Logger.verifyEquals(4, inventory.get("type").getAsInt(), "验证返回的type=4调整入库");
				}
			});
			
			
		} catch (Exception e) {
			Logger.fail(e);

		}
	}
}